package com.wzq.app.manager;

import com.wzq.app.BusApplication;
import com.wzq.app.constant.EventBusTag;
import com.wzq.app.model.UserInfo;
import com.wzq.app.utils.SPUtils;

import org.simple.eventbus.EventBus;

/**
 * @author : chenru
 * @功能描述:
 * @创建时间: 2017/5/4
 */

public class UserManager {
    private static UserManager userManager = new UserManager();
    private UserInfo mUserInfo;

    public static UserManager getInstance(){
        return userManager;
    }

    private final static String USER_KEY = "user";

    public void saveUser(UserInfo userInfo){
        if(userInfo!=null) {
            SPUtils.put(USER_KEY, BusApplication.getGson().toJson(userInfo));
            EventBus.getDefault().post("",EventBusTag.LOGIN_REFRESH);
        }
    }


    public UserInfo getUser(){
       if(mUserInfo == null) {
         String userJson = SPUtils.getString(USER_KEY);
         mUserInfo = BusApplication.getGson().fromJson(userJson,UserInfo.class);
       }
       return mUserInfo;
    }

    public boolean isLogin(){
        return getUser()!=null;
    }

    public void clear(){
        SPUtils.remove(USER_KEY);
        EventBus.getDefault().post("",EventBusTag.LOGIN_REFRESH);
        mUserInfo =null;
    }
}
